package hu.test.atacama;

import android.app.Activity;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

public class FragmentMain extends Fragment implements OnClickListener, OnLongClickListener {

	ImageView chooseCarButton, parkingButton, gpsButton, sosButton,
			showMapButton, arduinoButton, lockButton, unlockButton;

	private Common.mButtonClickListener buttonClickListener;

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		try {
			buttonClickListener = (Common.mButtonClickListener) activity;
		} catch (Exception ex) {
			Toast.makeText(activity.getApplicationContext(),
					"FragmentMain / onAttach: " + ex.toString(),
					Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View view = inflater.inflate(R.layout.fragment_main, container, false);

		arduinoButton = (ImageView) view.findViewById(R.id.imageArduino);
		gpsButton = (ImageView) view.findViewById(R.id.imageGPS);
		sosButton = (ImageView) view.findViewById(R.id.imageSos);
		showMapButton = (ImageView) view.findViewById(R.id.imageMap);
		parkingButton = (ImageView) view.findViewById(R.id.imageParking);
		chooseCarButton = (ImageView) view.findViewById(R.id.imageChooseCar);
		lockButton = (ImageView) view.findViewById(R.id.imageLock);
		unlockButton = (ImageView) view.findViewById(R.id.imageUnlock);
		
		arduinoButton.setOnClickListener(this);
		gpsButton.setOnClickListener(this);
		sosButton.setOnLongClickListener(this);
		//sosButton.setOnClickListener(this);
		showMapButton.setOnClickListener(this);
		parkingButton.setOnClickListener(this);
		chooseCarButton.setOnClickListener(this);
		lockButton.setOnClickListener(this);
		unlockButton.setOnLongClickListener(this);

		return view;
		// return super.onCreateView(inflater, container, savedInstanceState);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public void onClick(View v) {
		final ImageView clickedButton = (ImageView) v.findViewById(v.getId());
		clickedButton.setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);

		Runnable r = new Runnable() {
			@Override
			public void run() {
				clickedButton.clearColorFilter();
			}
		};

		Handler h = new Handler();
		h.postDelayed(r, 150);

		buttonClickListener.buttonClicked(clickedButton.getId());
	}

	@Override
	public boolean onLongClick(View v) {
		final ImageView clickedButton = (ImageView) v.findViewById(v.getId());
		clickedButton.setColorFilter(0xFF00FF00, PorterDuff.Mode.MULTIPLY);

		Runnable r = new Runnable() {
			@Override
			public void run() {
				clickedButton.clearColorFilter();
			}
		};

		Handler h = new Handler();
		h.postDelayed(r, 250);

		buttonClickListener.buttonClicked(clickedButton.getId());
		return true;
	}
	
	
}